﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DiracTCC.Business
{
    public class DiracReaderHelper
    {
        public static Chroma GetChromaFormat(int chroma)
        {
            switch (chroma)
            {
                case 0:
                    return Chroma.YUV444;
                case 1:
                    return Chroma.YUV422;
                case 2:
                    return Chroma.YUV420;
            }

            return Chroma.Unknow;
        }

        public static Chroma GetChromaFormat(string chroma)
        {
            if(chroma.Contains("444"))
                return Chroma.YUV444;
            else if(chroma.Contains("422"))
                return Chroma.YUV422;
            else if(chroma.Contains("420"))
                return Chroma.YUV420;
            else
                return Chroma.Unknow;
        }

        public static string GetPictureType(string ptype)
        {
            switch (ptype)
            {
                case "intra":
                    return "INTRA_PICTURE";
                case "mo_comp":
                    return "INTER_PICTURE";
            }

            return "Tipo Desconhecido";
        }

        public static string GetOutputPath(string input, string projectName)
        {
            string output = string.Empty;

            bool folderCreated = false;
            int folderId = 0;
            string folderPath =
                Path.Combine(Path.GetDirectoryName(input), projectName);

            while (!folderCreated)
            {
                if (folderId == 0)
                {
                    if (!Directory.Exists(folderPath))
                    {
                        Directory.CreateDirectory(folderPath);
                        folderCreated = true;
                        output = Path.Combine(folderPath, Path.GetFileNameWithoutExtension(input) + ".drc");
                    }
                    else
                    {
                        folderId++;
                    }
                }
                else
                {
                    var newPath = folderPath + "_" + folderId.ToString();
                    if (!Directory.Exists(newPath))
                    {
                        Directory.CreateDirectory(newPath);
                        folderCreated = true;
                        output = Path.Combine(newPath, Path.GetFileNameWithoutExtension(input) + ".drc");
                    }
                    else
                    {
                        folderId++;
                    }
                }
            }

            return output;
        }
    }
}
